package com.example.sxproject.mapper;

import com.example.sxproject.pojo.Company;
import com.example.sxproject.pojo.Payment;
import com.example.sxproject.pojo.SearchStr;

import com.example.sxproject.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PaymentMapper {


    //后台管理员查看全部账单
    @Select("select * from payment")
    List<Payment> selectAll();

    @Select("select * from payment where idNumber = #{idNumber}")
    List<Payment> selectByIdNumber(String idNumber);

    @Select({
            "<script>",
            "select *",
            "from payment",

            "<where>",
            "<if test='id!=null'>",
            "id=#{id} ",
            "</if>",

            "<if test='idNumber!=null'>",
            "AND idNumber =#{idNumber}",
            "</if>",

            "<if test='cardId!=null'>",
            "AND cardId =#{cardId}",
            "</if>",

            "<if test='money!=null'>",
            "AND money =#{money}",
            "</if>",

            "<if test='payDate != null'>",
            "payDate LIKE CONCAT('%', #{payDate}, '%')",
            "</if>",

            "<if test='transType!=null'>",
            "AND transType=#{transType}",
            "</if>",

            "<if test='payType!=null'>",
            "AND payType=#{payType}",
            "</if>",

            "<if test='recardId!=null'>",
            "AND recardId=#{recardId}",
            "</if>",

            "<if test='userId!=null'>",
            "AND userId=#{userId}",
            "</if>",

            "<if test='paymentStatus!=null'>",
            "AND paymentStatus=#{paymentStatus}",
            "</if>",


            "</where>",
            "</script>"
    })
    List<Payment> selectBySearchStr(SearchStr searchStr);

    //添加：添加一个记录
    @Insert("insert into payment" +
            "(idNumber, cardId, payDate, money, transType,payType,recardId,userId,paymentStatus)" +
            " values (#{idNumber}, #{cardId}, #{payDate}, #{money}, #{transType}, #{payType}, #{recardId}, #{userId},#{paymentStatus})")
    void addPayment(Payment payment);

    //更新：支付记录假删除
    @Update("update payment set paymentStatus = #{paymentStatus} " +
            "where id = #{id}")
    void deletePayment(Payment payment);

    //查询：通过userId查询支付记录
    @Select("SELECT * FROM payment WHERE userId = #{userId}")
    List<Payment> getPaymentsByUserId(Integer userId);

    @Update("UPDATE payment SET " +
            "idNumber = #{idNumber}, " +
            "cardId = #{cardId}, " +
            "transType = #{transType}, " +
            "payType = #{payType}, " +
            "recardId = #{recardId}, " +
            "userId = #{userId}, " +
            "payDate = #{payDate}, " +
            "money = #{money}" +
            "WHERE id = #{id}")
    void paymentMessageUpdate(Payment payment);

//管理员假删除操作
    @Update("update payment set paymentStatus = '2' " +
            "where id = #{id}")
    void PaymentDl(Integer id);
}


